﻿using System.Windows;
using System.Windows.Input;
using System.Windows.Media.Imaging;

namespace FAQ_UI
{
	/// <summary>
	/// ImageCropWindow.xaml 的交互逻辑
	/// </summary>
	public partial class ImageCropWindow : Window
	{
		private CroppedBitmap croppedBitmap;

		public ImageCropWindow(double widthHeightRatio)
		{
			InitializeComponent();
			DataContext = this;
			Crop.Background = Theme.BackBrush;
			Crop.setWidthHeightRatio(widthHeightRatio);
		}

		public CroppedBitmap getResult()
		{
			return croppedBitmap;
		}

		private void TextBlock_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
		{
			BitmapImage im = Crop.getImage();
			Int32Rect crop = Crop.getCropResult();
			croppedBitmap = new CroppedBitmap(im, crop);
			Close();
		}

		private void Window_Loaded(object sender, RoutedEventArgs e)
		{
			System.Windows.Forms.OpenFileDialog openFileDialog = new System.Windows.Forms.OpenFileDialog();
			if (openFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)//注意，此处一定要手动引入System.Window.Forms空间，否则你如果使用默认的DialogResult会发现没有OK属性
			{
				BitmapImage b = FAQUtil.loadBitmap(FAQUtil.readAllBytes(openFileDialog.FileName));
				if (b != null)
				{
					Crop.setImage(b);
				}
				else Close();
			}
			else Close();
		}
	}
}